package com.seventh.parkingrent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.seventh.parkingrent.common.Encrypt;
import com.seventh.parkingrent.common.MailUtil;
import com.seventh.parkingrent.controller.form.PasswordForm;
import com.seventh.parkingrent.controller.form.UserForm;
import com.seventh.parkingrent.domain.User;
import com.seventh.parkingrent.dao.UserMapper;
import com.seventh.parkingrent.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2021-08-24
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Resource
    private  UserMapper userMapper;

    @Override
    public User login(String username, String password) {
        log.info("进入登录业务{}",username+":"+password);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = userMapper.selectOne(wrapper);
        log.info(""+user);
        return user;
    }

    @Override
    public User getUserById(Integer id) {
      return   userMapper.selectById(id);
    }

    @Override
    public User findEmail(String email) {
        log.info("进入邮箱验证{}",email);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("email",email);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public boolean registerUser(UserForm userForm) throws Exception {

            log.info("进入注册业务{}",userForm);
            QueryWrapper<User> wrapper = new QueryWrapper<>();
            wrapper.eq("email",userForm.getEmail());
            User one = userMapper.selectOne(wrapper);
            if (null==one){
                User user = new User();
                BeanUtils.copyProperties(userForm,user);
                user.setUsername(userForm.getUsername());
                user.setPassword(Encrypt.md5(userForm.getPassword()));
                user.setEmail(userForm.getEmail());
                user.setPhone(userForm.getPhone());
                user.setState("0");
                user.setIsaudit("0");
                user.setIdentify("user");
                user.setIdcardPic("https://img2.baidu.com/it/u=3081972449,2659867711&fm=26&fmt=auto&gp=0.jpg");
                user.setUserPic("https://img0.baidu.com/it/u=1406515706,336510358&fm=26&fmt=auto&gp=0.jpg");
                user.setCode(Encrypt.md5("123"));
                int row = userMapper.insert(user);
                if (row>0) {
                    //发送激活邮件
                    MailUtil.sendMail("bqy1037239142@163.com","RWSOXUIQLSXFZKCK",
                            user.getEmail(),"用户激活","<a href='http://192.168.80.48:9090/updateState?email="+user.getEmail()+"'>点击激活："+user.getEmail()+"</a>");
                    return  true;
                }
            }
            return false;
    }


    @Override
    public boolean updateState(String email) {
        log.info("进去修改激活业务{}",email);
        String state="1";
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("email",email).set("state",state);
        int row = userMapper.update(null,wrapper);
        return row>0?true:false;
    }

    @Override
    public boolean updatebalance(double v,Integer id) {
        User user = userMapper.selectById(id);
        BigDecimal bigDecimal = new BigDecimal(Double.valueOf(v));
        user.setBalance(bigDecimal);
        int i = userMapper.updateById(user);
        return i>0;
    }

    @Override
    public boolean updatePassword(PasswordForm passwordForm) throws UnsupportedEncodingException, NoSuchAlgorithmException {

        log.info("进入修改密码业务{}",passwordForm);
        String s = Encrypt.md5(passwordForm.getPassword());
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",passwordForm.getUserId()).eq("password",s);
        User user = userMapper.selectOne(wrapper);
        if (null!=user) {
            String md5 = Encrypt.md5(passwordForm.getCheckpassword());
            UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("user_id",passwordForm.getUserId()).set("password",md5);
            int row = userMapper.update(null,updateWrapper);
            return row>0?true:false;
        }
        return false;
    }

    @Override
    public User findBalance(int userId) {

        log.info("{}",userId);
        return  userMapper.selectById(userId);

    }

    @Override
    public User findIdcard(String idcard) {
        log.info("{}",idcard);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("idcard", idcard);
        User user = userMapper.selectOne(wrapper);
        return user;
    }
}
